import api from '@/api/index'
const index = {
    state: {
        goodsDatas: {
            template: [],
            component: [],
            pictrue: [],
            icon: [],
            sdk: [],
        },
        searchList: [],//二类搜索字条
        searchData: {},//搜索框搜索到的数据
        hotWords: [],
        pagination: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 12, //一个展示几个
        },
        reload: true,
        recommendDatas: {
            template: [],
            component: [],
            pictrue: [],
            icon: [],
            sdk: []
        },
        goodsDetail: [
            {
            title: '设备终端',
            subTitle: [
                {
                    title: '全部',
                    selected: true
                },
                {
                    // title: '网页',
                    title: '后台',
                    selected: false
                },
                {
                    // title: '手机',
                    title: '冷色',
                    selected: false
                },
                {
                    title: '平板',
                    selected: false
                },
            ],
        },
        {
            title: '开发框架',
            subTitle: [{
                    title: '全部',
                    selected: true
                },
                {
                    title: 'BootStrap',
                    selected: false
                },
                {
                    title: 'Element',
                    selected: false
                },
                {
                    title: 'Vue',
                    selected: false
                },
                {
                    title: 'Wechat',
                    selected: false
                },
            ]
        },
        {
            title: '行业领域',
            subTitle: [{
                    title: '全部',
                    selected: true
                },
                {
                    title: '数据监控系统',
                    selected: false
                },
                {
                    title: '订单管理系统',
                    selected: false
                },
                {
                    title: '项目管理系统',
                    selected: false
                },
                {
                    title: '应用管理系统',
                    selected: false
                },
                {
                    title: '设备管理系统',
                    selected: false
                },
            ]
        },
        {
            title: '图标类型',
            subTitle: [{
                    title: '全部',
                    selected: true
                },
                {
                    title: '线型图标',
                    selected: false
                },
                {
                    title: '面型图标',
                    selected: false
                },
                {
                    title: '单色图标',
                    selected: false
                },
                {
                    title: '多色图标',
                    selected: false
                },
            ]
        },
        {
            title: '图片类型',
            subTitle: [{
                    title: '全部',
                    selected: true
                },
                {
                    title: '广告设计',
                    selected: false
                },
                {
                    title: 'UI设计',
                    selected: false
                },
                {
                    title: '插画配图',
                    selected: false
                },
                {
                    title: '摄影',
                    selected: false
                },
                {
                    title: '招聘海报',
                    selected: false
                },
                {
                    title: '背景图片',
                    selected: false
                },
            ]
        }
        ],
    },

    mutations: {
        saveSearchData: (state, {search_content, goodsList}) => {
            state.searchData = Object.assign({},{ [`与 ${search_content} 相关的宝贝`]: [...goodsList] });
        },
        saveHotWord: (state, hotWords) => {
            state.hotWords = [...hotWords];
        },
         //首页二级搜索栏操作
        saveGoodsDetail: (state, list) => {
            state.searchList = [...list];
            let data = {};
            let currentAll = [];
            list.forEach(item => {
                data[`${item}`] = [];
            });
            Object.keys(state.goodsDatas).forEach(type => {
                state.goodsDatas[type].forEach(good => {
                    currentAll.push(good);
                })
            });
            currentAll.forEach(good => {
                list.forEach(name => {
                    let patt = new RegExp(`${name}`,'i');
                    if(patt.test(good.goods_name)){
                        data[`${name}`].push(good);
                    }
                })
            });
            state.searchData = Object.assign({},{...data});
        },
        deleteGoodsDetail: (state,XWhich) => {
            state.goodsDetail.forEach(item => {
                item.subTitle.forEach(s => {
                    if(s.title === XWhich){
                        s.selected = false;
                    }
                })
            });
            let obj = {};
            Object.keys(state.searchData).forEach(key => {
                if(key !== 'undefined'){
                    obj[key] = state.searchData[key];
                }
            });
            state.searchData = Object.assign({}, obj);
        },
        saveIndexGoods: (state, goodsInfo) => {
            Object.keys(state.goodsDatas).forEach(key => {
                state.goodsDatas[key] = [];
            });
            goodsInfo.forEach( item => {
                switch(item.type_name){
                    case '模板':
                        state.goodsDatas.template.push(item);
                        break;
                    case '组件':
                        state.goodsDatas.component.push(item);
                        break;
                    case '图片':
                        state.goodsDatas.pictrue.push(item);
                        break;
                    case '图标':
                        state.goodsDatas.icon.push(item);
                        break;
                    case 'sdk':
                        state.goodsDatas.sdk.push(item);
                        break;
                }
            });
        },
        saveGoodsByType: (state, goodsInfo) => {
            Object.keys(state.goodsDatas).forEach( key => {
                state.goodsDatas[key] = [];
            });
            if(goodsInfo.length !== 0){
              switch(goodsInfo[0].type_name){
                case '模板':
                    state.goodsDatas.template = [...goodsInfo];
                    break;
                case '组件':
                    state.goodsDatas.component = [...goodsInfo];
                    break;
                case '图片':
                    state.goodsDatas.pictrue = [...goodsInfo];
                    break;
                case '图标':
                    state.goodsDatas.icon = [...goodsInfo];
                    break;
                case 'sdk':
                    state.goodsDatas.sdk = [...goodsInfo];
                    break;
              }
            }
        },
        savePagination: (state, paylaod) => {
            state.pagination = Object.assign(state.pagination,paylaod);
        },
        //先设置为false 再设置为true 手动更新分页数据 不可同时设置
        reloadPagination: (state, bool) => {
            state.reload = bool;
        },
        threeToGood: (state, goodsNum) => {
            Object.keys(state.goodsDatas).forEach(key => {
                state.goodsDatas[key].forEach(good => {
                    goodsNum.forEach(item => {
                        if(item.goods_id === good.goods_id){
                            good = Object.assign(good,{...item});
                        }
                    })
                });
            });
        },
        threeToGoodBySearch: (state, goodsNum) => {
            Object.keys(state.searchData).forEach(key => {
                state.searchData[key].forEach(good => {
                    goodsNum.forEach(item => {
                        if(item.goods_id === good.goods_id){
                            good = Object.assign(good,{...item});
                        }
                    })
                });
            });
        },
        saveRecommend: (state, payload) => {
           Object.keys(state.recommendDatas).forEach((key,index) => {
                state.recommendDatas[key]=[];
           });
           payload.recommendGoodsInfo.forEach(good => {
            payload.num.forEach(nums => {
                if(nums.goods_id === good.goods_id){
                    Object.assign(good,{ ...nums });
                }
            })
           });
           Object.keys(state.recommendDatas).forEach((key,index) => {
            state.recommendDatas[key].push(payload.recommendGoodsInfo[index]);
           });
        },
    },

    actions: {
        //首页接口
        async fetchIndexGoods({ commit }) {
            const { code, extend:{ result } } = await api.getIndexGoods();
            if(code === 200){
                commit('saveIndexGoods',result.goodsInfo);
                commit('threeToGood', result.goodsNum);
                commit('saveRecommend', { num: result.goodsNum, recommendGoodsInfo: result.recommendGoodsInfo});
            }
        },
        //根据类型获得商品
        async fetchGoodsByType({ commit },payload) {
            const { code, extend:{ result } } = await api.getTypeGoods(payload);
            if(code === 200){
                commit('saveGoodsByType',result.goodsInfo.list);
                commit('threeToGood', result.goodsNum);
                commit('savePagination', {
                    total:  result.goodsInfo.total,  //总共有几个
                    pageNum: result.goodsInfo.pageNum,  //当前页数
                });
                commit('reloadPagination', true);
            }
        },
        //热词接口
        async fetchHotword({ commit }) {
            const { code, extend:{ hotword } } = await api.getHotword();
            if(code === 200){
                commit('saveHotWord',hotword)
            }
        },
        //搜索接口
        async fetchSearchGoods({ commit },payload) {
            const { code, extend:{ result } } = await api.searchGoods(payload);
            if(code === 200){
                commit('saveSearchData', {search_content: payload.search_content, goodsList: result.goodsList.list });
                commit('threeToGoodBySearch', result.goodsNum);
                commit('savePagination', {
                    total:  result.goodsList.total,  //总共有几个
                    pageNum: result.goodsList.pageNum,  //当前页数
                });
                commit('reloadPagination',true);
            }else{
                commit('saveSearchData', {search_content: payload.search_content, goodsList: []})
                commit('savePagination', { size:  0, pageNum: 0 });
                commit('reloadPagination',true);
            }
        },
    }
}

export default index